//
//  LoginTools.m
//  Project_IOS
//
//  Created by Jeff on 2022/1/10.
//

#import "LoginTools.h"
//#import "UploadTool.h"
#import "GlobalDataTool.h"
#import "CodeViewController.h"
@interface LoginTools ()

@end

@implementation LoginTools
static LoginTools * _instance;
static dispatch_once_t onceToken;
+(LoginTools*)shareInstance {
    dispatch_once(&onceToken, ^{
        if(_instance == nil) {
            _instance = [[LoginTools alloc]init];
        }
    });
    return _instance;
}

static UIViewController *_nextSkipVC = nil;

+ (void)login:(NSString *)account pwd:(NSString *)pwd success:(void(^)(void))success {
    
    if (account.length != 11) {
//        [MBProgressHUD showMessage:@"请检查输入项！"];
        return;
    }
    
    if (pwd.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入密码！"];
        return;
    }
    
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_LOGIN] paramDic:@{@"account":account, @"password":pwd,@"device_id":[LoginTools getUUID]} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [LoginTools loginSuccess:dic[@"data"]];
//            [LoginTools requestForInit];
//            success();
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

+ (void)fastLogin:(void(^)(void))success {
    
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_TOURISTS_LOGIN] paramDic:@{@"device_id": [LoginTools getUUID]} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [LoginTools loginSuccess:dic[@"data"]];
//            [LoginTools requestForInit];
//            success();
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

+ (void)requestForInit {
    
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_INIT] paramDic:@{} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            NSDictionary *data = dic[@"data"];
//            if (data != nil) {
//                NSDictionary *uploaddata = data[@"uploaddata"];
//                UploadInfoModel *info = [UploadInfoModel yy_modelWithJSON:uploaddata];
//                UploadTool.shareInstance.infoMod = info;
//            }
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

+ (void)loginOut:(void(^)(void))success {
    
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_LOGOUT] paramDic:@{} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [self logoutInfo];
//            success();
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

+ (void)loginCancel:(NSString *)captcha success:(void(^)(void))success {
    
//    LoginUserModel *user = [LoginTools currentLoginUser];
//
//    if (captcha.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入验证码！"];
//        return;
//    }
//
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_ACCOUNT_CANCEL] paramDic:@{@"mobile": user.mobile, @"captcha":captcha} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [LoginTools logcancelSuccess];
//            success();
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

+ (void)loginBind:(NSString *)mobile captcha:(NSString *)captcha pwd:(NSString *)pwd success:(void(^)(void))success {
        
//    if (captcha.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入验证码！"];
//        return;
//    }
//
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_TOURISTS_BIND] paramDic:@{@"mobile":mobile, @"captcha":captcha, @"password":pwd ,@"device_id": [LoginTools getUUID]} finish:^(id responseObject) {
//        NSLog(@"------------  %@", responseObject);
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [LoginTools loginSuccess:dic[@"data"]];
//            success();
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
////        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}


+ (NSString *)getUUID{
//    NSString *UUID = [UICKeyChainStore stringForKey:@"column_uuid"];
//    if (!UUID) {
//        CFUUIDRef uuid = CFUUIDCreate(NULL);
//        UUID = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
//        CFRelease(uuid);
//        [UICKeyChainStore setString:UUID forKey:@"column_uuid"];
//    }
//    NSLog(@"UUID--------%@", UUID);
//    return UUID;
//    return @"F9811EBA-C5A6-4364-9E2B-AC6B61C68241";
    return @"fake_uuid_1234";
}

+ (BOOL)isLogin {
    LoginUserModel *loginModel = [LoginTools currentLoginUser];
    if (loginModel && loginModel.token.length > 0) {
        return true;
    }
    return false;
}

+ (void)loginSuccess:(NSDictionary *)dic {
    if (dic.count == 0) {
        return;
    }
    NSDictionary *info = dic[@"userinfo"];
    if (info.count == 0) {
        return;
    }
//    LoginUserModel *mod = [LoginUserModel yy_modelWithJSON:info];
//    [mod bg_saveOrUpdate];
//    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_keys_login_state_change object:nil];
//    if (_nextSkipVC) {
//        [UIApplication.sharedApplication.currentVC.navigationController pushViewController:_nextSkipVC animated:YES];
//        _nextSkipVC = nil;
//    }
}

+ (void)logoutInfo {
    LoginUserModel *user = [LoginTools currentLoginUser];
    user.token = @"";
//    [user bg_saveOrUpdate];
//    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_keys_login_state_change object:nil];
}

+ (void)logoutSuccess {
    UIViewController *currentVC = [GlobalDataTool shareInstance].currentVC;
//    if (!currentVC) {
//        currentVC = UIApplication.sharedApplication.currentVC;
//    }
    [LoginTools goLogin:currentVC nextVC:nil];
    [LoginTools logoutInfo];
}

+ (void)logcancelSuccess {
    LoginUserModel *user = [LoginTools currentLoginUser];
//    [[BGDB shareManager] deleteWithTableName:LoginUserTableName where:@[@"mobile",@"=",user.mobile] complete:nil];
//    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_keys_login_state_change object:nil];
}

+ (void)logPwdResetSuccess {
    [LoginTools logoutSuccess];
}

+ (LoginUserModel *)currentLoginUser {
//    NSArray* finfAlls = [LoginUserModel bg_findAll:LoginUserTableName];
//    if (finfAlls.count > 0) {
//        LoginUserModel *loginUser = finfAlls.lastObject;
//        return loginUser;
//    }
    return nil;
}


+ (void)goLogin:(UIViewController *)currentVC nextVC:(nullable UIViewController *)nextVC {
    _nextSkipVC = nextVC;
    
    UIViewController *vc;
    if (LoginTools.currentLoginUser == nil) {
        vc = [LoginTools creatRegistVC];
    }else {
        vc = [LoginTools creatLoginVC];
    }
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [currentVC.navigationController presentViewController:nav animated:YES completion:nil];
}

+ (void)goLogin:(UIViewController *)currentVC callback:(void(^)(BOOL))callback {
    LoginBaseViewController *vc;
    if (LoginTools.currentLoginUser == nil) {
        vc = [LoginTools creatRegistVC];
    }else {
        vc = [LoginTools creatLoginVC];
    }
    vc.loginCallback = callback;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    if ([currentVC isKindOfClass:UINavigationController.class]) {
        [currentVC presentViewController:nav animated:YES completion:nil];
    }else {
        [currentVC.navigationController presentViewController:nav animated:YES completion:nil];
    }
    
}


+ (LoginViewController *)creatLoginVC {
    return [[LoginViewController alloc] init];
}

+ (RegiestViewController *)creatRegistVC {
    return [[RegiestViewController alloc] init];
}

+ (BOOL)iSFirstLaunch {
    BOOL _launched = [[NSUserDefaults standardUserDefaults] boolForKey:@"first_launch"];
//    if (!_launched) {
//        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"first_launch"];
//    }
    return !_launched;
}

+ (void)invalideFirstLaunch {
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"first_launch"];
}


@end

